package com.dff.jrfw.utils;


import android.util.Base64;
import android.util.Log;

import java.io.UnsupportedEncodingException;

/**
 * @Title: 异或算法加密/解密工具类。
 * @description: 迁移util14
 * @Company: 北京九恒星科技股份有限公司
 * @author: yangs
 * @since: 2020-08-04 15:22
 * @version: 1.0
 **/
public class XORUtils {

    private XORUtils() {
    }

    /**
     * 异或加/解密，公式：str^c^c = str
     *
     * @param str
     * @param code
     * @return
     * @Description: TODO
     */
    public static String entOrDocXOR(String str, String code) {
        if (StringUtil.isEmpty(code) || StringUtil.isEmpty(str)) {
            return null;
        }
        int key = Integer.parseInt(code);
        StringBuilder sb = new StringBuilder();
        for (int i = 0, x = str.length(); i < x; i++) {
            int text2 = str.charAt(i) ^ key;
            sb.append((char) text2);
        }
        return sb.toString();
    }

//    public static void main(String[] args) {
////		long s = System.currentTimeMillis();
////		int k = 500;
////		for (int i = 0; i < k; i++) {
//        Date date = new Date();
//        String str = "ouyl1@@1";
//        String key = "392431";
//        System.out.println("key= " + key);
//        str = entOrDocXOR(str, key + "");
//        System.out.println("加密后的字符串:" + EncryptUtil.encodeUTF8StringBase64(str) + " 耗时" + ((new Date()).getTime() - date.getTime()));
//        Date date1 = new Date();
//        str = entOrDocXOR(str, key + "");
//        System.out.println("解密后的字符串:" + str + "耗时" + ((new Date()).getTime() - date1.getTime()));
////		}
////		System.out.println(k+"次加密解密耗时："+(System.currentTimeMillis()-s));
//    }


    /**
     * 字符Base64加密
     *
     * @param str
     * @return
     */
    public static String base64EncodeToString(String str) {
        try {
            return Base64.encodeToString(str.getBytes("UTF-8"), Base64.DEFAULT);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 字符Base64解密
     *
     * @param str
     * @return
     */
    public static String base64DecodeToString(String str) {
        try {
            return new String(Base64.decode(str.getBytes("UTF-8"), Base64.DEFAULT));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }



    /**
     * jwt解密
     *
     * @param JWTEncoded
     * @return
     */
    public static String jwtDecoded(String JWTEncoded) throws Exception {
        String bodyStr = null;
        try {
            String[] split = JWTEncoded.split("\\.");
            Log.d("JWT_DECODED", "Header: " + getJson(split[0]));
            Log.d("JWT_DECODED", "Body: " + getJson(split[1]));
            bodyStr = getJson(split[1]);
        } catch (UnsupportedEncodingException e) {
            //Error
        }
        return bodyStr;
    }


    private static String getJson(String strEncoded) throws UnsupportedEncodingException {
        byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
        return new String(decodedBytes, "UTF-8");
    }


}